package com.niit.controller;

import com.niit.entity.Userorders;
import com.niit.service.UserordersService;

import com.niit.utils.Result;
import org.springframework.boot.configurationprocessor.json.JSONArray;
import org.springframework.boot.configurationprocessor.json.JSONException;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.Map;

/**
 * (Userorders)表控制层
 *
 * @author makejava
 * @since 2024-06-18 15:45:07
 */
@RestController
@CrossOrigin(origins = "*")
@RequestMapping("userorders")
public class UserordersController {
    /**
     * 服务对象
     */
    @Resource
    private UserordersService userordersService;

    /**
     * 全查询
     *
     * @param userorders 筛选条件
     * @return 查询结果
     */
    @GetMapping("/All")
    public Result queryAll(Userorders userorders) {
        return this.userordersService.queryAll(userorders);
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    public Result queryById(@PathVariable("id") Integer id) {
        return this.userordersService.queryById(id);
    }

    /**
     * 新增数据
     *
     *
     * @return 新增结果
     * @ResponseBody会将获取的数据以json的格式返回
     */
    @PostMapping("/add")
    public Result add(@RequestBody Userorders userorders, HttpSession session ) throws JSONException {
        System.out.println("数据");
        System.out.println("name:"+userorders.toString());
        session.getAttribute("userID");
        System.out.println("用户Id"+session.getAttribute("userID"));
        Object userID = session.getAttribute("userID");
        userorders.setUserId(Integer.parseInt(String.valueOf(userID)));
        System.out.println(userorders.toString());
    return this.userordersService.insert(userorders);
    }

    /**
     * 修改数据
     *
     * @param userorders 实体
     * @return 编辑结果
     */
    @PutMapping
    public Result edit(@RequestBody Userorders userorders) {
        return this.userordersService.update(userorders);
    }

    /**
     * 删除数据
     *
     * @param id 主键
     * @return 删除是否成功
     */
    @DeleteMapping
    public Result deleteById(Integer id) {
        return this.userordersService.deleteById(id);
    }

}

